package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.dtos.SensitiveDTO;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.pojos.AdSensitive;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 29143
 */
@Service
@Transactional
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    @Autowired
    private AdSensitiveMapper adSensitiveMapper;

    /**
     * 查询敏感词
     * @param sensitiveDTO
     * @return
     */
    @Override
    public ResponseResult list(SensitiveDTO sensitiveDTO) {
        //判断参数是否合法
        if (sensitiveDTO==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查分页参数是否合法
        sensitiveDTO.checkParam();
        //构建分页参数
        IPage iPage=new Page(sensitiveDTO.getPage(),sensitiveDTO.getSize());
        //构建查询对象
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        //判断敏感词名称是否为空
        if (StringUtils.isNotBlank(sensitiveDTO.getName())) {
            //当敏感词名称不为空时,构建模糊查询条件
            wrapper.like(AdSensitive::getSensitives,sensitiveDTO.getName());
        }
        //封装条件
        IPage page=page(iPage,wrapper);
        //构建返回结果对象
        ResponseResult responseResult = new PageResponseResult(sensitiveDTO.getPage(),sensitiveDTO.getSize(),page.getTotal());
        responseResult.setData(page.getRecords());
        //返回结果
        return responseResult;
    }

    /**
     * 添加敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult add(AdSensitive adSensitive) {
        //判断参数是否为空
        if (adSensitive==null&&StringUtils.isBlank(adSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //创建查询条件对象
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        //调用查询方法,查看需要添加的敏感词是否存在
        int count = this.count(wrapper.eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (count>0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //设置实时时间
        adSensitive.setCreatedTime(new Date());
        //当需要添加的敏感词不存在时,调用添加方法
        this.save(adSensitive);
        return ResponseResult.okResult();
    }

    /**
     * 修改敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //判断参数是否合法
        if (adSensitive==null||adSensitive.getId()==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id参数查询敏感词信息
        AdSensitive sensitive = this.getById(adSensitive.getId());
        //判断查询到的结果是否为空
        if (sensitive==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"敏感词信息不存在");
        }
        //创建查询条件对象
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        int count = this.count(wrapper.eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (count>0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"敏感词已存在");
        }
        //添加实时时间
        adSensitive.setCreatedTime(new Date());
        //执行修改方法
        this.updateById(adSensitive);
        return ResponseResult.okResult();
    }

    /**
     * 根据id删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        //判断参数是否合格
        if (id==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //执行查询方法(根据id查询敏感词信息)
        AdSensitive sensitive = this.getById(id);
        //判断查询结果是否为空
        if (sensitive==null){
            //当查询结果为空时,返回错误信息
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"敏感词信息不存在");
        }
        //当查询结果不为空时,执行删除方法
        this.removeById(id);
        //返回结果
        return ResponseResult.okResult();
    }

    /**
     * 查询敏感词内容列表
     * @return
     */
    @Override
    public ResponseResult<List<String>> selectAllSensitives() {
        return ResponseResult.okResult(adSensitiveMapper.findAllSensitives());
    }
}
